
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from django_redis import get_redis_connection
from .validators import phone_validateor



#序列化类，这里用于校验手机号格式是否正确
class MessageSerializers(serializers.Serializer):
    phone = serializers.CharField(label="手机号",validators=[phone_validateor,])

    #钩子函数
    # def validate_phone(self,value):
    #     pass



class LoginSerializer(serializers.Serializer):
    phone = serializers.CharField(label="手机号",validators=[phone_validateor,])
    code = serializers.CharField(label="短信验证码")


    #钩子函数
    def validate_code(self,value):
        if len(value) != 4:
            raise ValidationError("短信格式错误")

        if not value.isdecimal():
            raise ValidationError("短信格式错误")

        phone = self.initial_data.get("phone")
        conn = get_redis_connection()
        code = conn.get(phone)

        if not code:
            raise ValidationError("验证码过期")

        if value != code.decode('utf-8'):
            raise ValidationError("验证码错误")

        return value